package vip.kqsy.cloud.blog.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.kqsy.cloud.blog.constant.em.BlogInteractiveTypeEm;
import vip.kqsy.cloud.blog.server.mapper.BlogInteractiveMapper;
import vip.kqsy.cloud.blog.server.pojo.dto.InteractiveOptionDTO;
import vip.kqsy.cloud.blog.server.pojo.entity.BlogInteractive;
import vip.kqsy.cloud.blog.server.service.BlogInteractiveService;

import java.time.LocalDateTime;

/**
 * <p>
 * 博客交互表 服务实现类
 * </p>
 *
 * @author cenjunjie
 * @since 2023-05-03
 */
@Service
public class BlogInteractiveServiceImpl extends ServiceImpl<BlogInteractiveMapper, BlogInteractive> implements BlogInteractiveService {

    @Override
    public void like(Long blogId, Long userId) {
        BlogInteractive blogInteractive = new BlogInteractive();
        blogInteractive.setBlogId(blogId);
        blogInteractive.setUserId(userId);
        blogInteractive.setType(BlogInteractiveTypeEm.LIKE.getCode());
        blogInteractive.setCreateTime(LocalDateTime.now());
        save(blogInteractive);
    }

    @Override
    public void option(InteractiveOptionDTO dto) {
        if (dto.getOption()) {
            BlogInteractive blogInteractive = BeanUtil.toBean(dto, BlogInteractive.class);
            blogInteractive.setCreateTime(LocalDateTime.now());
            save(blogInteractive);
        } else {
            BlogInteractive one = lambdaQuery()
                    .eq(BlogInteractive::getBlogId, dto.getBlogId())
                    .eq(BlogInteractive::getUserId, dto.getUserId())
                    .eq(BlogInteractive::getType, dto.getType())
                    .one();
            removeById(one.getId());
        }
    }
}
